package leetcode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * 描述:21. 合并两个有序链表
 *
 * @author Madison You
 * @created 0:20
 */
public class Easy_21_MergeTwoLists {

    public static void main(String[] args) {
        int[] ints1 = {1, 2, 4};
        int[] ints2 = {1, 3, 4};
        ListNode node1 = Mid_2_AddTwoNumbers.createNode(ints1);
        ListNode node2 = Mid_2_AddTwoNumbers.createNode(ints2);
        ListNode listNode = mergeTwoLists(node1, node2);
        System.out.println(listNode);
    }

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode ListNode = new ListNode();
        ListNode other = ListNode;
        while (list1 != null || list2 != null) {
            ListNode node = null;
            if (list1 != null && list2 != null) {
                if (list1.val <= list2.val) {
                    node = new ListNode(list1.val);
                    list1 = list1.next;
                } else {
                    node = new ListNode(list2.val);
                    list2 = list2.next;
                }
            } else if (list1 != null && list2 == null) {
                node = new ListNode(list1.val);
                list1 = list1.next;
            } else {
                node = new ListNode(list2.val);
                list2 = list2.next;
            }
            other.next = node;
            other = other.next;
        }

        return ListNode.next;
    }

}
